package echat.future.cn.echat.my.view;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.CompoundButton;

import com.hyphenate.chat.EMClient;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivitySettingBinding;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.UserBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;
import echat.future.cn.echat.my.weight.dialog.ChoseDialog;
import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.baselibgxh.utils.SharedPreference;

public class SettingActivity extends BaseActivity<ActivitySettingBinding>{

    public static final String EXTRA_TYPE = "EXTRA_TYPE";


    private boolean isFree = false;


    @Override
    public int layoutId() {
        return R.layout.activity_setting;
    }

    @Override
    public IBaseNavagation navagation() {
        return new AdapterNavagation(this).setTitle("设置", 16, R.color.white).setBack(R.drawable.btn_back).setNavagationBackgroudColor(R.color.color_161823);
    }

    @Override
    public void initView() {
//        if(UserBean.TYPE_ANCHOR == UserHelper.getUser().getUser_role()){
//            bind.llFree.setVisibility(View.VISIBLE);
//        }else{
//            bind.llFree.setVisibility(View.GONE);
//        }
        bind.llClean.setOnClickListener(v -> showClen());

        bind.checkFree.setImageResource(MyApplication.getInstance().isXianmian?R.drawable.kai:R.drawable.guan);
        bind.checkFree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateFree(MyApplication.getInstance().isXianmian?0:1);
            }
        });

        bind.llHelp.setOnClickListener(v -> HelpCenterActivity.openActivity(this,HelpCenterActivity.class));
        bind.logout.setOnClickListener(v -> logout());
    }

    private Dialog clenDialog = null;
    private void showClen(){
        if(clenDialog == null)
         clenDialog =  new ChoseDialog.Builder(this).content("您确定清除缓存么，清理后将删除 所有聊天记录及视频消息。")
                .rightListener(v->{
                    clenDialog.dismiss();
                toast("清除缓存成功");}).Builder();

        clenDialog.show();
    }

    private Dialog freeDialog = null;
    public void check(){
        if(freeDialog == null)
            freeDialog =  new ChoseDialog.Builder(this).content("你的账户余额不足100E币，暂时 无法开启限免，请保证余额在100E币以上")
                    .rightListener(v->{
                        freeDialog.dismiss();
                        updateFree(1);
                      }).cancelColor(Color.parseColor("#999999")).comfirmColor(Color.parseColor("#333333")).Builder();

        freeDialog.show();
    }

    /**
     *
     * @param free 限免开关：【1：限免，0：不限免】
     */
    private void updateFree(int free){
        new HttpUtilsRequst<>(this,HttpUtilsRequst.retrofitClient.create(UserApi.class).updateFreeStatus(UserHelper.getToken(),free))
                .setmOnRequstSuccessListener(o -> {
                    toast("设置限免成功");
                    MyApplication.getInstance().isXianmian = !MyApplication.getInstance().isXianmian;
                    bind.checkFree.setImageResource(MyApplication.getInstance().isXianmian?R.drawable.kai:R.drawable.guan);
//                    finish();
                }).setmOnRequstFaileListener((code, msg) -> {
                    toast("设置限免失败");
//                    finish();
                })
                .start();
    }

    private void logout(){
        UserHelper.cleanAccount();
        UserHelper.clean();

        Intent intent = new Intent(this,LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);

    }
}
